import { history } from 'umi';
import { message } from 'antd';
import api from '../../../services';
const { queryAllComment, delComment, reviewComment, reviewAllComment } = api;

export default {
  namespace: 'commentManage',

  state: {
    commentList: [],
  },

  subscriptions: {
    setupHistory({ dispatch, history }) {  // eslint-disable-line
      return history.listen(({ pathname, state }) => {
        if (pathname === '/manageCenter/commentManage') {
          dispatch({ type: "queryComment" });
          return;
        }
      })
    },
  },
  effects: {
    *queryComment({ }, { put, call, select }) {
      const result = yield call(queryAllComment);
      if (result.code === 0) {
        yield put({ type: "updateState", payload: { commentList: result.data } });
      }
      else {
        message.error(result.message, 1);
      }
    },
    *reviewComment({ payload }, { put, call, select }) {
      const result = yield call(reviewComment, payload);
      if (result.code === 0) {
        yield put({ type: "queryComment" });
        message.success("审核完成");
      }
      else {
        message.error(result.message, 2);
      }
    },
    *reviewAllComment({ payload }, { put, call, select }) {
      const result = yield call(reviewAllComment, payload);
      if (result.code === 0) {
        yield put({ type: "queryComment" });
        message.success("一键审核成功！");
      }
      else {
        message.error(result.message, 3);
      }
    },
    *delComment({ payload }, { put, call, select }) {
      const result = yield call(delComment, payload);
      if (result.code === 0) {
        yield put({ type: "queryComment" });
        message.success("删除成功");
      }
      else {
        message.error(result.message, 4);
      }
    },
  },
  reducers: {
    updateState(state, action) {
      return { ...state, ...action.payload };
    }
  }
};